package com.ruoyi.productCheck.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.productCheck.domain.CheckInformation;
import com.ruoyi.productCheck.domain.ProductReport;
import com.ruoyi.productCheck.service.ProductReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/productReport")
public class ProductReportController extends BaseController {

    @Autowired
    ProductReportService productReportService;

    /**
     * 保存成品检验报告
     */
    @Log(title = "保存成品检验报告", businessType = BusinessType.INSERT)
    @PostMapping("/addReport")
    public AjaxResult addReport(@RequestBody ProductReport productReport) {
        productReport.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = productReportService.addReport(productReport);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 查询成品检验报告
     */
    @GetMapping("/reportList")
    @ResponseBody
    public TableDataInfo reportList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                                       @RequestParam(value = "endTime", required = false) String endTime) {
        ProductReport productReport = new ProductReport();
        productReport.setBeginTime(beginTime);
        productReport.setEndTime(endTime);
        startPage();
        productReport.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<ProductReport> list = productReportService.reportList(productReport);
        return getDataTable(list);
    }

    /*
       查询已经做完的成品检验报告
*/
    @GetMapping("/selectReport")
    public ProductReport selectReport(@RequestParam(value = "reportId", required = false) Long reportId) {
        ProductReport productReport = productReportService.selectReport(reportId);
        return productReport;
    }

    /**
     * 删除成品检验报告
     * *
     */
    @Log(title = "删除成品检验报告", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{reportId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("reportId") Long reportId) {
        productReportService.remove(reportId);
        return AjaxResult.success("删除成功");
    }

    /*
    查询审核完的成品检验报告
      */
    @GetMapping("/auditReport")
    public Map<String, Object> auditReport() {
        Map<String, Object> resMap = new HashMap<>();
        List<ProductReport> productReports = productReportService.auditReport(SecurityUtils.getDeptId());
        resMap.put("productReports", productReports);
        return resMap;
    }

    /*
      编辑成品检验报告
         */
    @PostMapping("/updateReport")
    @ResponseBody
    public AjaxResult updateReport(@RequestBody ProductReport productReport) {
        int num = productReportService.updateReport(productReport);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }


}
